/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.funda.xmppserver.roster;

import com.funda.xmppserver.XMPPClient;
import com.funda.xmppserver.XMPPInfo;
import com.funda.xmppserver.mysql.DBManager;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author kurinchi
 */
public class Roster {

    XMPPClient client;
    Connection conn;
    Statement smt;
    ResultSet rs;

    Map<Object, Object> rosteritems=new HashMap<Object, Object>();

    public Roster(XMPPClient client) {
        this.client = client;
        conn=DBManager.getConn();
    }

    public Map<Object, Object> getRosteritems() {
        return rosteritems;
    }

    public void setRosteritems(Map<Object, Object> rosteritems) {
        this.rosteritems = rosteritems;
    }



    public String getItems()
    {
        String itemlist="";
        try {
            smt = conn.createStatement();
            rs=smt.executeQuery("select * from roster where userid='"+client.getUsername()+"'");

            
            while(rs.next())
            {
                String itemjid=rs.getString("item")+"@"+XMPPInfo.getHOST();
                String subscription=rs.getString("subscription");
                rosteritems.put(itemjid, subscription);
                itemlist+="<item "+(subscription.equalsIgnoreCase("none")?"ask='subscribe'":"")+" jid='"+itemjid+"' subscription='"+subscription+"'/>";
            }
            //System.out.println(itemlist);
        } catch (SQLException ex) {
            Logger.getLogger(Roster.class.getName()).log(Level.SEVERE, null, ex);
        }
        return itemlist;
    }

}
